from django.db import models

from apps.project.models import ProjectModel
from apps.user.models import UserModel
from apps.interface.models import InterfaceModel
from apps.function.models import FunctionModel
from apps.performance.models import PerformanceModel


# Create your models here.
class CaseModel(models.Model):
    class Meta:
        db_table = 'case'
        verbose_name = '用例'
        verbose_name_plural = verbose_name

    TYPE_CHOICES = ((1, '功能测试'), (2, '接口测试'), (3, '性能测试'))
    PRIORITY_CHOICES = ((0, 'P0'), (1, 'P1'), (2, 'P2'), (3, 'P3'), (4, 'P4'), (5, 'P5'), (6, 'P5'))
    STATUS_CHOICES = ((0, '通过'), (1, '失败'), (2, '阻塞'), (3, '跳过'), (4, '未开始'))

    project_id = models.ForeignKey(to=ProjectModel, on_delete=models.CASCADE, verbose_name='项目id')
    user_id = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='创建者id',
                                related_name='admin_user')
    last_executor_name = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='最后执行人',
                                           related_name='last_executor_user')
    name = models.CharField(max_length=30, null=False, verbose_name='用例名')
    create_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    type = models.IntegerField(choices=TYPE_CHOICES, null=False, verbose_name='用例类型')
    interface_id = models.ForeignKey(to=InterfaceModel, null=True, blank=True, on_delete=models.CASCADE,
                                     verbose_name='接口id')
    function_id = models.ForeignKey(to=FunctionModel, null=True, blank=True, on_delete=models.CASCADE,
                                    verbose_name='功能测试id')
    performance_id = models.ForeignKey(to=PerformanceModel, null=True, blank=True, on_delete=models.CASCADE,
                                       verbose_name='性能测试id')
    priority = models.IntegerField(choices=PRIORITY_CHOICES, null=False, default=0, verbose_name='优先级')
    comments = models.CharField(max_length=512, null=False, blank=True, default='', verbose_name='备注')
    status = models.IntegerField(choices=STATUS_CHOICES, default=4, verbose_name='状态')

    def get_type_str(self):
        res = ''
        for t in self.TYPE_CHOICES:
            if t[0] == self.type:
                res = t[1]
                break
        return res

    def __str__(self):
        return self.name + '(' + self.get_type_str() + ')'
